package cn.zl.web.impl.streamproxy;

import cn.yunbamboos.id.SnowflakeIdGenerator;
import cn.yunbamboos.mybatis.conditions.Conditions;
import cn.yunbamboos.rest.service.anno.RestServiceMapping;
import cn.yunbamboos.rest.service.dto.Page;
import cn.yunbamboos.rest.service.dto.out.BaseOutDTO;
import cn.yunbamboos.rest.service.transaction.Transaction;
import cn.zl.config.ZLConfig;
import cn.zl.model.StreamProxy;
import cn.zl.web.dto.streamproxy.AddStreamProxyInDTO;
import cn.zl.web.dto.streamproxy.AddStreamProxyOutDTO;
import cn.zl.web.dto.streamproxy.DeleteStreamProxyInDTO;
import cn.zl.web.dto.streamproxy.DeleteStreamProxyOutDTO;
import cn.zl.web.dto.streamproxy.PlayStreamProxyInDTO;
import cn.zl.web.dto.streamproxy.PlayStreamProxyOutDTO;
import cn.zl.web.dto.streamproxy.QueryStreamProxyByPageInDTO;
import cn.zl.web.dto.streamproxy.QueryStreamProxyByPageOutDTO;
import cn.zl.web.dto.streamproxy.UpdateStreamProxyInDTO;
import cn.zl.web.dto.streamproxy.UpdateStreamProxyOutDTO;
import cn.zl.web.impl.StreamService;
import cn.zl.web.inter.streamproxy.IStreamProxyService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service("StreamProxyServiceImpl")
public class StreamProxyServiceImpl extends StreamService implements IStreamProxyService {

    @Resource
    private SnowflakeIdGenerator snowflakeIdGenerator;
    @Resource
    private ZLConfig zlConfig;

    @RestServiceMapping(url = "/web/stream-proxy/queryByPage", name = "条件分页查询拉流列表服务", in = QueryStreamProxyByPageInDTO.class, out = QueryStreamProxyByPageOutDTO.class)
    @Override
    public BaseOutDTO queryByPage(QueryStreamProxyByPageInDTO in) {
        // 创建分页对象
        Page<StreamProxy> page = new Page<>(in.getPageNo(), in.getPageSize());
        // 添加动态条件
        Conditions conditions = new Conditions();
        // 查询总数
        int total = streamProxyMapper.countForPage(page, conditions);
        // 查询当前页码数据
        List<StreamProxy> list = streamProxyMapper.queryForPage(page, conditions);
        // 处理性别 用户状态 角色
        page.setRows(list).setTotal(total);
        return new QueryStreamProxyByPageOutDTO(page);
    }

    @RestServiceMapping(url = "/web/stream-proxy/add", name = "添加新的拉流信息服务", in = AddStreamProxyInDTO.class, out = AddStreamProxyOutDTO.class, transaction = Transaction.WRITE)
    @Override
    public BaseOutDTO add(AddStreamProxyInDTO in){
        StreamProxy proxy = in.getStreamProxy();
        this.checkAppAndStream(proxy.getApp(), proxy.getStream());
        proxy.setProxyId(snowflakeIdGenerator.nextIdStr());
        LocalDateTime now = LocalDateTime.now();
        proxy.setUpdateTime(now);
        proxy.setCreateTime(now);
        streamProxyMapper.insert(proxy);
        return new AddStreamProxyOutDTO();
    }

    @RestServiceMapping(url = "/web/stream-proxy/update", name = "更新拉流信息服务", in = UpdateStreamProxyInDTO.class, out = UpdateStreamProxyOutDTO.class, transaction = Transaction.WRITE)
    @Override
    public BaseOutDTO update(UpdateStreamProxyInDTO in){
        StreamProxy streamProxy = in.getStreamProxy();
        LocalDateTime now = LocalDateTime.now();
        streamProxy.setCreateTime(now);
        streamProxyMapper.update(streamProxy);
        return new UpdateStreamProxyOutDTO();
    }

    @RestServiceMapping(url = "/web/stream-proxy/delete", name = "删除拉流信息服务", in = DeleteStreamProxyInDTO.class, out = DeleteStreamProxyOutDTO.class, transaction = Transaction.WRITE)
    @Override
    public BaseOutDTO delete(DeleteStreamProxyInDTO in){
        in.forEach(id->{
            streamProxyMapper.deleteByPrimaryKey(id);
        });
        return new DeleteStreamProxyOutDTO();
    }

    @RestServiceMapping(url = "/web/stream-proxy/play-stream", name = "播放拉流视频服务", in = PlayStreamProxyInDTO.class, out = PlayStreamProxyOutDTO.class, transaction = Transaction.WRITE)
    @Override
    public BaseOutDTO playStream(PlayStreamProxyInDTO in){
        String realUrl = in.getSession("real_url");
        URI url = URI.create(realUrl);
        String host = url.getHost();
        PlayStreamProxyOutDTO out = new PlayStreamProxyOutDTO();
        String proxyId = in.getProxyId();
        Optional<StreamProxy> optional = streamProxyMapper.queryByPrimaryKey(proxyId);
        optional.ifPresent(stream -> {
            out.setHost(host);
            out.setApp(stream.getApp());
            out.setStream(stream.getStream());
            out.setConfig(zlConfig);
        });
        return out;
    }

}
